//
//  BIZArticleDetailCell.m
//  ConsultantVersion
//
//  Created by  小龙 on 2016/12/12.
//  Copyright © 2016年 com.jieshuibao1. All rights reserved.
//

#import "BIZArticleDetailCell.h"
#import "BIZArticleModel.h"

@interface BIZArticleDetailCell ()<UIWebViewDelegate>

@property (weak, nonatomic) IBOutlet UIImageView *headerView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;

@property (weak, nonatomic) IBOutlet UIImageView *certificationImageView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *certificationWidth;

@property (weak, nonatomic) IBOutlet UILabel *personalDetailLabel;
@property (weak, nonatomic) IBOutlet UILabel *levelLabel;


@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *titleLabelWidth;

@property (weak, nonatomic) IBOutlet UILabel *timelabel;
@property (weak, nonatomic) IBOutlet UIButton *tagBtn;


@property (weak, nonatomic) IBOutlet NSLayoutConstraint *webViewHeight;
@property (weak, nonatomic) IBOutlet UIView *shareView;

@property (nonatomic, assign) CGFloat expectdHeight;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *qWidth;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *qzWidth;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *wcWidth;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *wtWidth;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *sinaWidth;

@property (weak, nonatomic) IBOutlet UIButton *deleteBtn;

@property (weak, nonatomic) IBOutlet UILabel *redListLabel;

@property (weak, nonatomic) IBOutlet UIView *redListHeadView;

@property (weak, nonatomic) IBOutlet UIButton *redListBtn;
@property (weak, nonatomic) IBOutlet UIView *redPacketBtnView;
@property (weak, nonatomic) IBOutlet UIView *redPacketListView;

@end


@implementation BIZArticleDetailCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.webView.autoresizingMask = (UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
    self.webView.multipleTouchEnabled = NO;
    self.webView.autoresizesSubviews = YES;
    self.webView.opaque = NO;
    self.webView.scrollView.scrollEnabled = NO;
    self.webView.scrollView.showsHorizontalScrollIndicator = NO;
    self.webView.scrollView.showsVerticalScrollIndicator = NO;
    self.webView.scrollView.bounces = NO;
    self.webView.scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    self.webView.delegate = self;
    
    _expectdHeight = 0;
}


- (void)setArticleModel:(BIZArticleModel *)articleModel
{
    _articleModel = articleModel;
    // 头像
    [SDWebImageTool imageView:self.headerView
                    imageName:articleModel.headImg placeHolder:@"default-head" showProgress:YES];
    self.nameLabel.text = articleModel.cname;
    
    self.certificationWidth.constant = 17;
    self.levelLabel.text = articleModel.fname;
    self.personalDetailLabel.text = articleModel.position;
    
    
    self.titleLabel.text = articleModel.title;
    self.timelabel.text = [NSDate stringFromTimestamp:articleModel.createDate];
}

- (void)setHtml:(NSString *)html
{
    if ([_html isEqualToString:html]) return;
    
    _html = [html copy];
//    DeLog(@"---- %@", html);
    if (!self.webView.isLoading && _html != nil && ![_html isEqualToString:@""]) {
        NSString *HTML = [_html stringByReplacingOccurrencesOfString:@"\n" withString:@"<br>"];
        [self.webView loadHTMLString:HTML baseURL:nil];
    }
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    if(!webView.isLoading){
        
        CGFloat height = [webView sizeThatFits:CGSizeZero].height + 10;
        if (height != _webViewHeight.constant) {
            
            _webViewHeight.constant = height;
            _expectdHeight = height;
//            DeLog(@"height %f", height);
            if (self.delegate && [self.delegate respondsToSelector:@selector(articleDetailCell:heightDidChange:)]) {
                [self.delegate articleDetailCell:self heightDidChange:height];
            }
        }
    }
    
    
}
    
//
//    CGFloat height00 = [webView sizeThatFits:CGSizeZero].height;
////    CGFloat height01 = webView.scrollView.contentSize.height;
////    CGFloat height02 = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"] floatValue];
////    CGFloat height03 = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight"] floatValue];
//
////    DeLog(@"--height00 %f, height01 %f, height02 %f, height03 %f", height00, height01, height02, height03);



- (void)stopLoading
{
    if ([self.webView isLoading]) {
        [self.webView stopLoading];
    }
    
    [self cleanCacheAndCookie];
}

- (void)cleanCacheAndCookie{
    //清除cookies
    NSHTTPCookie *cookie;
    NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    for (cookie in [storage cookies]){
        [storage deleteCookie:cookie];
    }
    
    //清除UIWebView的缓存
    [[NSURLCache sharedURLCache] removeAllCachedResponses];
    NSURLCache * cache = [NSURLCache sharedURLCache];
    [cache removeAllCachedResponses];
    [cache setDiskCapacity:0];
    [cache setMemoryCapacity:0];
    
}


- (IBAction)briberyBtnClicked:(UIButton *)sender {
    if (self.delegate && [self.delegate respondsToSelector:@selector(articleDetailCellbriberyBtnDidClicked:)]) {
        [self.delegate articleDetailCellbriberyBtnDidClicked:self];
    }
}


- (IBAction)headCoverBtnClicked:(UIButton *)sender {
    if (self.delegate && [self.delegate respondsToSelector:@selector(originalDetailCellHeadCoverBtnDidClicked)]) {
        [self.delegate originalDetailCellHeadCoverBtnDidClicked];
    }
}



@end
